package easytasks.ui;

import java.util.Calendar;

import easytasks.data.DataSetup;
import easytasks.data.DataManager;
import easytasks.logic.DateFormatter;
import easytasks.logic.DateFormatter.DateAsStringType;
import easytasks.logic.TaskNotificationManager;
import easytasks.main.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity 
{
	private AppMenu appMenu;
	private ListView listMenuOptions;
	
	private DataSetup dataInit;
	private DataManager dataManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        appMenu = new AppMenu(this);
        listMenuOptions = (ListView)findViewById(R.id.listMainOptions);
        listMenuOptions.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
			{
				Intent taskListIntent = null;
				
				switch(position)
				{
					case 0:
						taskListIntent = new Intent(MainActivity.this, TaskListActivity.class);
						taskListIntent.putExtra("action", "today");
						break;
					case 1:
						taskListIntent = new Intent(MainActivity.this, TaskListActivity.class);
						taskListIntent.putExtra("action", "tomorrow");
						break;
					case 2:
						taskListIntent = new Intent(MainActivity.this, TaskListActivity.class);
						taskListIntent.putExtra("action", "current-week");	
						break;
					case 3:
						taskListIntent = new Intent(MainActivity.this, TaskListActivity.class);
						taskListIntent.putExtra("action", "next-week");	
						break;
					case 4:
						taskListIntent = new Intent(MainActivity.this, TaskCategoryListActivity.class);
						break;
					default:
						taskListIntent = new Intent(MainActivity.this, TaskListActivity.class);
						taskListIntent.putExtra("action", "all"); // Default value
						break;
				}
				
				startActivity(taskListIntent);
			}
		});
        
        dataInit = new DataSetup(this);
        dataManager = new DataManager(dataInit, PreferenceManager.getDefaultSharedPreferences(this));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)	
    {
    	super.onCreateOptionsMenu(menu);
    	return appMenu.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	return appMenu.onOptionsItemSelected(item);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	refresh();
    }

	private void refresh() 
	{
		setTitle(getString(R.string.main_today) + " " + (new DateFormatter(this)).getTimeAsString(Calendar.getInstance().getTime(), DateAsStringType.WEEKDAYDAY_AND_LONGMONTHANDHOUR, true));
		
		String[] menuItems = getResources().getStringArray(R.array.main_options);
		menuItems[0] = menuItems[0].replace("#0#", String.valueOf(dataManager.getTodayPendingTaskCount())); // Today's task count
		menuItems[1] = menuItems[1].replace("#0#", String.valueOf(dataManager.getTomorrowPendingTaskCount())); // Tomorrow's task count
		menuItems[2] = menuItems[2].replace("#0#", String.valueOf(dataManager.getCurrentWeekPendingTaskCount())); // Current week's task count
		menuItems[3] = menuItems[3].replace("#0#", String.valueOf(dataManager.getNextWeekPendingTaskCount())); // Next week's task count
		menuItems[5] = menuItems[5].replace("#0#", String.valueOf(dataManager.getPendingTaskCount())); // All task count
    	
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems);	
    	listMenuOptions.setAdapter(adapter);
    	
    	appMenu.refresh();
    	
    	// Update notification alarm
        new TaskNotificationManager(this).updateNotificationAlarm();
	}
}